/**
 * Created by huibei on 17/2/8.
 */

export default class DateUtil {
    static dateDistanceByNow = (dateString) => {
        var timeDesc = ''
        var time = dateString.replace(/-/g, "/")
        var date = new Date(time)
        var now = new Date()
        var t = now.getTime() - date.getTime();
        var d = 0;
        var h = 0;
        var m = 0;
        var s = 0;
        if (t >= 0) {
            d = Math.floor(t / 1000 / 60 / 60 / 24);
            h = Math.floor(t / 1000 / 60 / 60 % 24);
            m = Math.floor(t / 1000 / 60 % 60);
            s = Math.floor(t / 1000 % 60);
        }
        if (d > 0) {
            timeDesc = dateString
        }
        else if (h > 0) {
            timeDesc = h + '小时前'
        }
        else if (m > 0) {
            timeDesc = m + '分钟前'
        }
        else if (s > 0) {
            timeDesc = '刚刚'
        }
        return timeDesc
    }

    /**
     * 获取制定日期的日期 默认是今天 1 明天 2 后天  -1 昨天
     * @param byDay
     * @returns {*}
     */
    static getDate = (byDay = 0) =>{
        let now = new Date();
        let dayIndex = now.getDate();
        now.setDate(dayIndex + byDay);
        let weekIndex = now.getDay();

        let week = '';

        switch (weekIndex) {
            case 0:
                week =  '周日';
                break;
            case 1:
                week =  '周一';
                break;
            case 2:
                week =  '周二';
                break;
            case 3:
                week =  '周三';
                break;
            case 4:
                week =  '周四';
                break;
            case 5:
                week =  '周五';
                break;
            case 6:
                week =  '周六';
                break;
        }
        let  day = now.getFullYear().toLocaleString()+'-'+(now.getMonth()+1).toString()+'-'+now.getDate().toString();
        return {
            weekIndex:week,
            day:day
        }
    }
}
